package com.jwthhealth_pub.old.common.Address;

import android.util.Log;

import com.j256.ormlite.dao.Dao;
import com.jwthhealth_pub.app.App;
import com.jwthhealth_pub.old.common.Address.ormlite.OrmManager;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by Administrator on 2017/5/10.
 */

public class AddressDao {
    private static AddressDao instance;
    private Dao<Address, String> addressesDao;

    public AddressDao() {
        try {
            OrmManager ormManager = new OrmManager(App.mContext);
            addressesDao = ormManager.getAddressesDao();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public synchronized static AddressDao getInstance() {

        if (instance == null) {
            instance = new AddressDao();
        }

        return instance;
    }

    public List<Address> getProvinces(){
        List<Address> addresses = null;

        try {
            addresses = addressesDao.queryForEq("pid", "0");
        } catch (SQLException e) {
            Log.d("AddressDao", e.toString());
        }

        return addresses;
    }

    public List<Address> getCitys(String spid) {
        List<Address> addresses = null;
        try {
            addresses = addressesDao.queryBuilder().where().eq("spid", spid).and().eq("pid", "1").query();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return addresses;
    }

    public List<Address> getCountries(String cid) {
        Log.d("AddressDao", cid);
        List<Address> addresses = null;
        try {
            addresses = addressesDao.queryBuilder().where().like("cid", cid+"%").and().eq("pid", "2").query();
        } catch (SQLException e) {
            Log.d("AddressDao", e.toString());
        }

        return addresses;
    }
}
